import requests
from douban.settings import IP_LIST_FILE
from functools import partial
from threading import Thread
from concurrent.futures import ThreadPoolExecutor
from utils.proxy_api import write_proxy_file

proxies = dict()
pool = ThreadPoolExecutor(max_workers=200)
# with open(IP_LIST_FILE,'r') as f:
#     text = f.readlines()


# proxis_list = [i[:-1] for i in text[:100]]

# url='https://movie.douban.com/'
url = 'http://cip.cc'


# 下面这样访问是会报错,因为没有用代理服务器，直接访问有防火墙
# proxis_list = {
#     "https":"https://190.52.164.252:8080",
#     "http":"http://190.52.164.252:8080",
# }
def determinate(ip_str, proxis_list):
    try:
        res = requests.get(url, proxies=proxis_list, timeout=10)
        with open(IP_LIST_FILE + "ok", 'aw') as f:
            f.write(ip_str)
        print(ip_str)
        print('ok')
    except Exception:
        pass


pools_list = list()

while True:
    # print('start new')
    # write_proxy_file()
    with open(IP_LIST_FILE, 'r') as f:
        while True:
            ip_str = f.readline()
            ip = ip_str[:-1]
            proxis_list = {
                "https": ip,
                "http": ip[0:4] + ip[5:],
                #"http": ip,
            }
            # Thread(target=determinate,args=(ip_str,proxis_list)).start()
            tmp = pool.submit(partial(determinate, ip_str), (proxis_list))
            pools_list.append(tmp)

            if not ip_str:
                break
        for i in pools_list:
            i.result()
        print('over')

# res=requests.get(url,proxies=proxis_list,timeout=10)
# #res=requests.get(url,timeout=10)
# print(res.text)
